﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.Model.Common;
using Chinaer.Core.BaseCore.Extensions;
using Chinaer.Core.Helper;
 
namespace Chinaer.Bll
{
    public class Address : Repository, Chinaer.IBll.IAddress
    {
        Chinaer.IDao.IAddressDAO addressDAO = new Chinaer.Dao.AddressDAO();
        public JsonResult Add(DTO.AddressDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }

            JsonResult jsonResult = new JsonResult();
            Model.Entities.Model_Address model_Address = new Model.Entities.Model_Address();
            if (t != null)
            {
                ///表示未登录
                if (string.IsNullOrEmpty(t.ApiToken) || t.ApiToken.Trim().Length == 0)
                {
                    //表示没有被登陆
                    jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.NonLogin;
                    jsonResult.ErrorMessage = "用户未登录";
                    jsonResult.Success = false;
                    return jsonResult;
                }
                else if (!string.IsNullOrEmpty(t.ApiToken))
                {
                    //表示的是输入了ApiToken值
                   IBll.ICustomer customer=new Bll.Customer();
                    bool flag=customer.CheckApitokenIsUseful(t.ApiToken);
                    if (flag)
                    {
                        //表示是有效的用户
                        DTO.CustomerDTO customerDTO = customer.GetByApiToken(t.ApiToken);
                        if (customerDTO != null)
                        {
                            model_Address.CustomerID = customerDTO.ID;
                        }
                    }
                    else
                    { 
                    //表示不是有效的用户
                        jsonResult.Success = false;
                        jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.UserNotExists;//表示用户不存在
                        jsonResult.ErrorMessage = "用户不存在";
                        return jsonResult;
                    }
                }
            }
            
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_Address);
            if (string.IsNullOrEmpty(model_Address.ID) || model_Address.ID.Trim() == "")
            {
                model_Address.ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID();
            }
            jsonResult.Success = addressDAO.Add(model_Address);
            return jsonResult;
        }

        public JsonResult Delete(DTO.AddressDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            JsonResult jsonResult = new JsonResult();
            Model.Entities.Model_Address model_Address = new Model.Entities.Model_Address();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_Address);
            jsonResult.Success = addressDAO.Delete(model_Address);
            return jsonResult;
        }

        public JsonResult Delete(string id)
        {
            JsonResult jsonResult = new JsonResult();
            jsonResult.Success = addressDAO.Delete(id);
            return jsonResult;
        }

        public JsonResult Update(DTO.AddressDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            JsonResult jsonResult = new JsonResult();
            Model.Entities.Model_Address model_Address = new Model.Entities.Model_Address();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_Address);
            jsonResult.Success = addressDAO.Update(model_Address);
            return jsonResult;
        }

        public List<DTO.AddressDTO> GetAddressByCustomerID(string customerID)
        {

            List<Model.Entities.Model_Address> model_ListAddress = new List<Model.Entities.Model_Address>();
            List<DTO.AddressDTO> listAddressDTO = new List<DTO.AddressDTO>();
            model_ListAddress = addressDAO.GetAddressByCustomerID(customerID);
            lock (model_ListAddress)
            {
                foreach (var item in model_ListAddress)
                {
                    DTO.AddressDTO addressDTO = new DTO.AddressDTO();
                    Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(item, addressDTO);
                    listAddressDTO.Add(addressDTO);
                }
            }
            return listAddressDTO;
        }


        public DTO.AddressDTO GetByID(string id)
        {
            Model.Entities.Model_Address model_Address = addressDAO.GetByID(id);
            DTO.AddressDTO addressDTO = new DTO.AddressDTO();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_Address, addressDTO);
            return addressDTO;
        }
    }
}
